//
//  SmallKonwledgeViewController.m
//  ProjectA
//
//  Created by dlios on 15/3/5.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SmallKonwledgeViewController.h"

@interface SmallKonwledgeViewController ()

@end

@implementation SmallKonwledgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"小知识";
    
    //判断网络状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    if ([wifi currentReachabilityStatus] == NotReachable || [conn currentReachabilityStatus] == NotReachable) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提 示" message:@"网络请求失败, 请检查网络连接" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        
        [alert show];
        
        
    }//if
    
#pragma mark--TableView
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:(UITableViewStylePlain)];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.view addSubview:self.myTableView];
    
    self.allDataArray = [NSMutableArray array];

    self.isUpLoading = NO;
    
    self.nextPage = 2; //标记下一页
    
    //集成刷新控件
    [self addHeader];
    [self addFooter];
    
    
    
}//did load

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
} //section number




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allDataArray.count;
} //row number


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KnowledgeDetailVC *detailVC = [[KnowledgeDetailVC alloc] init];
    
    KnowledgeModal *modal = [self.allDataArray objectAtIndex:indexPath.row];
    
    detailVC.identify = [modal.ID integerValue]; //传 ID
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
    
    
}//点击 cell 触发方法


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
} //height


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identify = @"reuse";
    KnowledgeTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if (cell == nil) {
        cell = [[KnowledgeTableviewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identify];
        
    }
    
    KnowledgeModal *modal = [self.allDataArray objectAtIndex:indexPath.row];
    
    NSLog(@"cell test1 %@", modal.ID);
    
    cell.titleLabel.text = modal.title;
    cell.indexLabel.text = modal.ID;
    
    
    return cell;
    
} //cell


- (void)addHeader{
    //    __unsafe_unretained typeof(self) vc = self;
    __block SmallKonwledgeViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        
        NSLog(@"下拉刷新，开始网络请求第一页数据");
        
        vc.isUpLoading = NO;
        
        
        
        [vc getData:1];         //请求数据
        
        
        
        
    }];
    
    //自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
    
} //下拉刷新



- (void)addFooter{
    //    __unsafe_unretained typeof(self) vc = self;
    __block SmallKonwledgeViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        vc.isUpLoading = YES;
        
        [vc getData: (vc.nextPage++)];   //请求下一页的数据
        
    }];
    
} //上拉加载更多


#if 1
-(void)getData:(NSInteger)ID{
    
    NSString *dataString = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.tips.list.php?keywords=&p=%ld&pagesize=20&from=com.hp.zkwc&version=1.0",ID];
    
    
    NSLog(@"string = %@", dataString);
    
    
    
    [AFNDownLoader getDataDownLoad:dataString block:^(id myData) {
        NSDictionary *dataDic = [NSDictionary dictionary];
        dataDic = myData;
        
        
        
        if (self.isUpLoading == NO) {
            [self.allDataArray removeAllObjects];
        }
        
        
        
        NSArray *array = [dataDic objectForKey:@"results"];
        
        for (NSDictionary *dic  in array) {
            
            KnowledgeModal *modal = [[KnowledgeModal alloc] init];
           
            [modal setValuesForKeysWithDictionary:dic];
            
            [self.allDataArray addObject:modal];
            
            NSLog(@"===> %@", modal.title);
        }
        
       
        
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
        
    }]; //block
    
    
} //AFN
#endif

    



@end

















